import store from "../store/store.js"
import Vue from 'vue'

window.userInfo = {
  id: "888888"
};

export const togClass = (el, className) => {
  if (el.classList) {
    el.classList.toggle(className);
  } else {
    let classes = el.className.split(' ');
    let existingIndex = classes.indexOf(className);

    if (existingIndex >= 0)
      classes.splice(existingIndex, 1);
    else
      classes.push(className);

    el.className = classes.join(' ');
  }
};

let vrv = vrv || {};

export const vrvInit = {
  userInfo: "",
  init(){
    let self = this;
    vrv.init({
      debug: false
    });
    vrv.ready(() => {

    });
  }
};

export const getHeader = {
  type: "",
  txt: "",
  init(v){
    let str = "";
    switch (v) {
      case 1:
        str = `今日快报`;
        break;
      case 11:
        str = `添加选题`;
        break;
      case 12:
        str = `快报详情`;
        break;
      case 20:
        str = `${this.txt}管理`;
        break;
      case 2:
        str = `${this.txt}详情`;
        break;
      case 21:
        str = `通讯录`;
        break;
      case 111:
        str = `请示领导`;
        break;
    }
    return str;
  },
  getHeaderTxt(){
    let h = "";
    switch (parseInt(this.type)) {
      case 1:
        h = "今日快报";
        break;
      case 2:
        h = "事件";
        break;
      case 3:
        h = "预警";
        break;
    }
    return h;
  }
};

window.bus = new Vue();

window.ajax_ = {
  url: "/Cyberspace/sys/",
  timeOut: 3000,
  error(){
    mui.toast('服务器没有响应，请稍后重试！');
  },
  loading(val){
    store.commit("_loadingShow", val);
  },
  getType(url, data, call){
    let self = this;
    mui.ajax(this.url + url, {
      data: data,
      dataType: 'json',
      type: 'get',
      timeout: this.timeOut,
      success(data){
        call(data.result);
      },
      error(){
        self.error();
      }
    });
  },
  postType(url, data, call){
    let self = this;
    mui.ajax(this.url + url, {
      data: data,
      dataType: 'json',
      type: 'post',
      timeout: this.timeOut,
      success(data){
        call(data);
      },
      error(){
        self.error();
      }
    });
  },
  deleteType(url, data, call){
    let self = this;
    mui.ajax(this.url + url, {
      data: data,
      dataType: 'json',
      type: 'delete',
      timeout: this.timeOut,
      success(data){
        call(data);
      },
      error(){
        self.error();
      }
    });
  },
  putType(url, data, call){
    let self = this;
    self.loading(true);
    mui.ajax(this.url + url, {
      data: data,
      dataType: 'json',
      type: 'put',
      timeout: this.timeOut,
      success(data){
        self.loading(false);
        call(data);
      },
      error(){
        self.loading(false);
        self.error();
      }
    });
  },
  confirmPutType(url, data, call){
    confirm_(() => {
      this.putType(url, data, call);
    });
  }
};

window.alert_ = (call) => {
  mui.alert('处理成功', '', () => {
    call && call();
  });
};

window.confirm_ = (call) => {
  mui.confirm('是否确认处理？', '提示', ['取消', '确认'], (e) => {
    if (e.index == 1) {
      call && call();
    }
  });
};

window.formatDate = (date, fmt) => {
  date = date == undefined ? new Date() : date;
  date = typeof date == 'number' ? new Date(date) : date;
  fmt = fmt || 'yyyy-MM-dd HH:mm:ss';
  var obj =
    {
      'y': date.getFullYear(), // 年份，
      'M': date.getMonth() + 1, // 月份，
      'd': date.getDate(), // 日期
      'q': Math.floor((date.getMonth() + 3) / 3), // 季度
      'w': date.getDay(), // 星期，
      'H': date.getHours(), // 24小时制
      'h': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, // 12小时制
      'm': date.getMinutes(), // 分钟
      's': date.getSeconds(), // 秒
      'S': date.getMilliseconds() // 毫秒
    };
  var week = ['日', '一', '二', '三', '四', '五', '六'];
  for (var i in obj) {
    fmt = fmt.replace(new RegExp(i + '+', 'g'), function (m) {
      var val = obj[i] + '';
      if (i == 'w') return (m.length > 2 ? '星期' : '周') + week[val];
      for (var j = 0, len = val.length; j < m.length - len; j++) val = '0' + val;
      return m.length == 1 ? val : val.substring(val.length - m.length);
    });
  }
  return fmt;
};

window.getUrlParam = (name) => {
  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  let params = "";
  let val = "";
  if (window.location.search) {
    params = window.location.search.substr(1).match(reg);
  } else if (window.location.hash && window.location.hash.split("?")[1]) {
    params = window.location.hash.split("?")[1].match(reg);
  }
  if (params != null) {
    val = decodeURI(params[2])
  }
  return val;
};


// 全局组件
import loadingInPage from "@/components/loadingInPage.vue"
import contentBody from "@/components/contentBody.vue"

Vue.component("loadingInPage", loadingInPage);
Vue.component("contentBody", contentBody);

